<?php

class Defined_Language{
	
	private $sLangKey;
	private $oTranslate;
	private $oLocation;
	
	static $oSingleTon;
	
	function __destruct(){
		unset($this->sLangKey);	
		unset($this->oTranslate);	
		unset($this->oLocation);	
	}
	
	function __construct(){
	}
	
	function initZendTranslate(){
		
		$arrConfig=Zend_Registry::get('config_site');
		$sDefaultKey=$arrConfig['default_language'];
		$this->sLangKey= $sDefaultKey;     
		
	    $this->oLocation = new Zend_Locale();
		$this->oTranslate = new Zend_Translate(
    		array('adapter' => 'ini', 'content' =>LIBS_PATH.'/Language','scan' =>Zend_Translate::LOCALE_FILENAME)
		);
		unset($arrConfig); unset($sDefaultKey);
	}
	
	public function load(){
		if(is_null($this->oTranslate)){
			$this->install();
		}
		Zend_Registry::set('Zend_Translate', $this->oTranslate);		
	}
	
	public function getInstant(){
		if(!isset(self::$oSingleTon)){
			$c=__CLASS__;
			self::$oSingleTon=new $c();
			unset($c);
		}
		return self::$oSingleTon;
	}
	
	public function getLangKey(){
		return $this->sLangKey;
	}
	
	public function install($sLang=''){
		# find a change lang request from client
		if(trim($sLang)!=''){
			$this->setLang($sLang);
		}else{
			if(isset($_COOKIE["lang_index"])){
				$this->getLang();
			}
		}
		unset($sLang);
		# load language from Zend and return object language
		return $this->callZendTranslate();
		
	}
	
	private function callZendTranslate(){
		$this->oLocation->setLocale($this->sLangKey);
		Zend_Registry::set('Zend_Locale', $this->oLocation);

		$this->oTranslate->setLocale($this->oLocation);
		if (!$this->oTranslate->isAvailable($this->oLocation->getLanguage())) { 
			$this->oTranslate->setLocale($this->sLangKey);
		    $this->setLang($this->sLangKey);
		}
		return $this->oTranslate;
	}
	
	private function setLang($sLang){
		setcookie('lang_index',$sLang,time()+30*24*60*60,'/');
		$this->sLangKey=$sLang;
		unset($sLang);
	}
	
	private function getLang(){
		$this->sLangKey=$_COOKIE["lang_index"];
	}
	
	
	
}
?>